#include "QRWSketchCreateWidget.h"

#include "Core/MainWindow.h"
#include "QSketchManager.h"
#include <TopoDS.hxx>
#include <QMessageBox>

QRWSketchCreateWidget::QRWSketchCreateWidget(MainWindow* inMainWindow, ShapeOperation* inOperation, QWidget* parent)
	: QRSWidgetBase(inMainWindow, inOperation, parent)
	, ui(new Ui::QRWSketchCreateWidgetClass())
{
	ui->setupUi(this);

	ui->basic->installEventFilter(this);
	ui->normalWidget->installEventFilter(this);
	ui->directionWidget->installEventFilter(this);
	ui->centerWidget->installEventFilter(this);

	connect(ui->confirm, &QPushButton::clicked, this, &QRWSketchCreateWidget::onConfirm);
	connect(ui->cancel, &QPushButton::clicked, this, &QRWSketchCreateWidget::close);
	connect(m_mainWindow, &MainWindow::OnSelectedSubShapes, this, &QRWSketchCreateWidget::OnSelectedSubShapes);
	connect(ui->comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &QRWSketchCreateWidget::OnTargetComboIndexChange);
	connect(m_mainWindow, &MainWindow::OnLeftButtonDown, this, &QRWSketchCreateWidget::onLeftButtonDown);

	m_right = new RWTripleFloatInput(ui->dirX, ui->dirY, ui->dirZ);
	m_center = new RWTripleFloatInput(ui->centerX, ui->centerY, ui->centerZ);
	m_normal = new RWTripleFloatInput(ui->n_x, ui->n_y, ui->n_z);

}

QRWSketchCreateWidget::~QRWSketchCreateWidget()
{
	delete ui;
	delete m_right;
	delete m_center;
	delete m_normal;
}

void QRWSketchCreateWidget::OnSelectedSubShapes(QIVtkSelectionPipeline* actor)
{
	if (m_mainWindow->GetSelectedShapes().size() == 0)
	{
		return;
	}

	const TopoDS_Shape& selectedShape = m_mainWindow->GetSelectedShapes().back();
	// 选取的不是边
	if (selectedShape.ShapeType() != TopAbs_EDGE)
	{
		return;
	}

	TopoDS_Edge selectedEdge = TopoDS::Edge(selectedShape);
	gp_Dir edgeDirection = GetEdgeDirection(selectedEdge);

	// 选择平面法线
	if (focusIndex == 0)
	{
		m_normal->SetValue(edgeDirection);
	}
	// 选择右向量
	else if (focusIndex == 1)
	{
		m_right->SetValue(edgeDirection);
	}
}

void QRWSketchCreateWidget::closeEvent(QCloseEvent* event)
{
	disconnect(m_mainWindow, &MainWindow::OnSelectedSubShapes, this, &QRWSketchCreateWidget::OnSelectedSubShapes);

	m_mainWindow->SetSelectionMode(SM_None);
	m_mainWindow->SubWidgetClose(bIsConfirmed);
}

bool QRWSketchCreateWidget::eventFilter(QObject* obj, QEvent* event)
{
	QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
	if (event->type() == QEvent::MouseButtonPress && mouseEvent->button() == Qt::LeftButton)
	{
		if (obj == ui->basic)
		{
			ui->normalWidget->setStyleSheet("background-color: #FFFFFF;");
			ui->directionWidget->setStyleSheet("background-color: #FFFFFF;");
			ui->centerWidget->setStyleSheet("background-color: #FFFFFF;");

			focusIndex = -1;
            m_mainWindow->SetSelectionMode(SM_None);
		}
		else if (obj == ui->normalWidget && focusIndex != 0)
		{
			ui->normalWidget->setStyleSheet("background-color:rgb(255,178,102)");
			ui->directionWidget->setStyleSheet("background-color: #FFFFFF;");
			ui->centerWidget->setStyleSheet("background-color: #FFFFFF;");

			focusIndex = 0;
			m_mainWindow->SetSelectionMode(SM_Edge);
		}
		else if (obj == ui->directionWidget && focusIndex != 1)
		{
			ui->directionWidget->setStyleSheet("background-color:rgb(255,178,102)");
			ui->normalWidget->setStyleSheet("background-color: #FFFFFF;");
			ui->centerWidget->setStyleSheet("background-color: #FFFFFF;");

			focusIndex = 1;
			m_mainWindow->SetSelectionMode(SM_Edge);
		}
		else if (obj == ui->centerWidget && focusIndex != 2)
		{
			ui->centerWidget->setStyleSheet("background-color:rgb(255,178,102)");
			ui->normalWidget->setStyleSheet("background-color: #FFFFFF;");
			ui->directionWidget->setStyleSheet("background-color: #FFFFFF;");

			focusIndex = 2;
			m_mainWindow->SetSelectionMode(SM_Vertex);
		}
	}

	return QObject::eventFilter(obj, event);
}

void QRWSketchCreateWidget::onConfirm()
{
	bIsConfirmed = true;
	if (!IsNearlyZero(m_normal->GetValue().Dot(m_right->GetValue())))
	{
		QMessageBox::warning(this, tr("Warning"), tr("法线与右向量不垂直!"));
		return;
	}
	m_mainWindow->GetSketchManager()->CreateSketch(m_center->GetValue(), m_normal->GetValue(), m_right->GetValue());
	close();
}

void QRWSketchCreateWidget::OnTargetComboIndexChange(int index)
{
	if (index == 0)
	{
		m_normal->SetValue(gp_Dir(0, 0, 1));
		m_right->SetValue(gp_Dir(1, 0, 0));
	}
	else if (index == 1)
	{
		m_normal->SetValue(gp_Dir(0, 1, 0));
		m_right->SetValue(gp_Dir(0, 0, 1));
	}
	else if (index == 2)
	{
		m_normal->SetValue(gp_Dir(1, 0, 0));
		m_right->SetValue(gp_Dir(0, 1, 0));
	}
}

void QRWSketchCreateWidget::onLeftButtonDown()
{
	bool bPickerSuccess = false;
	gp_Pnt PickPos = gp_Pnt();
	m_mainWindow->GetVertexPickerTargetPos(bPickerSuccess, PickPos);
	if (bPickerSuccess)
	{
		OnPickPosition(PickPos);
	}
}

void QRWSketchCreateWidget::OnPickPosition(const gp_Pnt& position)
{
	m_center->SetValue(position);
}
